//
//  MMBProgressView.m
//  MMBangADDemoProject
//
//  Created by CuiPanJun on 14-7-28.
//  Copyright (c) 2014年 CuiPanJun. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>

#import "MMBProgressView.h"


@interface MMBProgressViewTrackView : UIView

@property(nonatomic, assign) BOOL usingImagePattern;
@property(nonatomic, strong) UIImage *progressImage;

@end

@implementation MMBProgressViewTrackView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.contentMode = UIViewContentModeRedraw;
    }
    return self;
}

- (void)setUsingImagePattern:(BOOL)usingImagePattern {
    _usingImagePattern = usingImagePattern;
    [self setNeedsDisplay];
}

- (void)setProgressImage:(UIImage *)progressImage {
    _progressImage = progressImage;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    if (_usingImagePattern) {
        [self.progressImage drawAsPatternInRect:self.bounds];
    } else {
        [self.progressImage drawInRect:self.bounds];
    }
}

@end

@interface MMBProgressView () {
    MMBProgressViewTrackView *_progressTrackView;
    UIImage *_cachedProgressImage;  // self Synthesized Progress Image by drawing pattern progress image in a bigger rect
    UIImageView *_progressImageView;
    UIImageView *_trackImageView;
}

@end

@implementation MMBProgressView

- (id)initWithFrame:(CGRect)frame style:(MMBProgressViewStyle)style {
    self = [self initWithFrame:frame];
    if (self) {
        self.clipsToBounds = YES;
        _trackImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _trackImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self addSubview:_trackImageView];
        _progressImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _progressImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _progressImageView.contentMode = UIViewContentModeLeft;
        _progressImageView.clipsToBounds = YES;
        [self addSubview:_progressImageView];
        self.style = style;
    }
    
    return self;
}

- (void)updateViewsForStyle {
    
    CGFloat borderWidth = 0.0f;
    CGFloat cornerRadius = 0.0f;
    UIColor *borderColor = nil;
    
    switch (self.style) {
        case MMBProgressViewStylePlain:
            
            break;
        case MMBProgressViewStyleRoundBorder:
            
            borderWidth = 1.0f;
            cornerRadius = CGRectGetHeight(self.frame)/2;
            borderColor = [UIColor whiteColor];
            
            break;
        case MMBProgressViewStyleRoundDoubleBorder:
            borderWidth = 1.0f;
            cornerRadius = CGRectGetHeight(self.frame)/2;
            borderColor = [UIColor colorWithHex:kMainColor alpha:1.];
            _progressImageView.layer.cornerRadius = (self.height - 2)/2.;
            _progressImageView.layer.borderWidth = 2;
            _progressImageView.layer.borderColor = [UIColor clearColor].CGColor;
            _progressImageView.clipsToBounds = YES;
            _trackImageView.hidden = YES;
        default:
            break;
    }
    
    self.layer.borderWidth = borderWidth;
    self.layer.cornerRadius = cornerRadius;
    self.layer.borderColor = borderColor.CGColor;
    
}

- (void)setProgress:(float)progress {
    [self setProgress:progress animated:NO];
}

- (void)setProgress:(CGFloat)progress animated:(BOOL)animated {
    if (progress < 0.0f) {
        progress = 0.0f;
    }
    
    if (progress > 1.0f) {
        progress = 1.0f;
    }
    
    _progress = progress;
    if (animated) {
        [UIView beginAnimations:@"MMBangProgressViewAnimation" context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationBeginsFromCurrentState:YES];
        
    }
    _progressTrackView.frame = CGRectMake(0, 0, _progress * CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
    if (_style == MMBProgressViewStyleRoundDoubleBorder) {
        _progressImageView.frame = CGRectMake(_progressImageView.layer.borderWidth, _progressImageView.layer.borderWidth, _progress * (CGRectGetWidth(self.bounds)- _progressImageView.layer.borderWidth*2), CGRectGetHeight(self.bounds)-_progressImageView.layer.borderWidth*2);
    } else {
        _progressImageView.frame = CGRectMake(0, 0, _progress * CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
    }
    
    if (animated) {
        [UIView commitAnimations];
    }
}

- (void)setProgressImage:(UIImage *)progressImage {
    [self setProgressImage:progressImage isPattern:NO];
}

- (void)setProgressImage:(UIImage *)progressImage isPattern:(BOOL)imageIsPattern {
    _progressImage = progressImage;
    _progressTrackView.progressImage = _progressImage;
    _progressTrackView.usingImagePattern = imageIsPattern;
    if (imageIsPattern) {
        [self rebuildProgressImage];
    } else {
        _cachedProgressImage = _progressImage;
        _progressImageView.image = _cachedProgressImage;
    }
}

- (UIImage *)trackImage {
    return _trackImageView.image;
}

- (void)setTrackImage:(UIImage *)trackImage {
    _trackImageView.image = trackImage;
}

- (void)setStyle:(MMBProgressViewStyle)style {
    _style = style;
    [self updateViewsForStyle];
}

#pragma mark - Set Frame
- (void)setFrame:(CGRect)frame {
    CGRect oldFrame = self.frame;
    if (CGRectGetWidth(oldFrame) < CGRectGetWidth(frame)) {
        [self rebuildProgressImage];
    }
    [super setFrame:frame];
}

- (void)rebuildProgressImage {
    _cachedProgressImage = [[self class] tileFillImageWithImage:self.progressImage inRect:self.bounds];
    _progressImageView.image = _cachedProgressImage;
}

+ (UIImage *)tileFillImageWithImage:(UIImage *)patternImage inRect:(CGRect)rect {
    UIImage *image = patternImage;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGContextDrawTiledImage(context, rect, patternImage.CGImage);
    [patternImage drawAsPatternInRect:rect];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
@end

